<template>
  <myHeader :crumbs="crumbs"></myHeader>
  <div class="body">
    <div class="body-option">
      <div v-for="(item, index) in vipStrategyData.chargedPrices" :key="index" class="body-option-item"
        @click="payFn(item, index)">
        <div class="body-option-item-box">
          <div class="body-option-item-box-label">
            <img class="vip" src="../assets/img/pay-vip.png" alt="">
            <span class="title">{{ item.title }}</span>
          </div>
          <img class="selected" src="../assets/img/pay-selected.png" alt="" v-show="index === payIndex">
        </div>
      </div>
    </div>
    <el-dialog v-model="payVisible" title="微信支付" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" center>
      <img :src="payUrl" alt="" style="display: block;margin: auto;">
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="payVisible = false;payIndex = ''">取消</el-button>
          <el-button type="primary" @click="payQueryFn">我已支付</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import myHeader from '@/components/header.component.vue';
import { ElMessage } from 'element-plus';
import { ref, getCurrentInstance } from 'vue';
const { $api } = getCurrentInstance().appContext.config.globalProperties;
import { useRoute, useRouter } from 'vue-router';
const route = useRoute()
const router = useRouter()
const vipStrategyData = ref({})
const payIndex = ref('')
const payUrl = ref('')
const payVisible = ref(false)
const crumbs = ref([{
  name: '支付',
  url: ''
}])

// 获取价格选项
const getPayOptionFn = () => {
  $api.getExerciseVipStrategyApi({
    subjectId: route.params.subject
  }).then((res) => {
    if (res.errno === 0) {
      if (res.result) {
        vipStrategyData.value = res.result;
      }
      return false;
    }
    ElMessage.error(res.errmsg)
  })
}
getPayOptionFn()
// 支付
const payFn = (val, ind) => {
  if (payIndex.value === '') {
    payIndex.value = ind;
    $api.buyVipApi({
      subjectId: vipStrategyData.value.subjectId,
      priceId: val.priceId
    }).then((res) => {
      if (res.errno === 0) {
        $api.nativepayApi({
          orderNo: res.result
        }).then((res) => {
          if (res.errno === 0) {
            payUrl.value = `https://api.jkb100.com/api/qr.png?content=${res.result}`;
            payVisible.value = true;
            return false;
          }
          ElMessage.error(res.errmsg)
        })
        return false;
      }
      ElMessage.error(res.errmsg)
    })
  }
}
// 我已支付
const payQueryFn = () => {
  payVisible.value = false;
  payIndex.value = '';
  router.back()
}
</script>

<style lang="scss" scoped>
.body {
  width: 1200px;
  min-height: 560px;
  margin: 30px auto 0;
  background-image: url('../assets/img/pay-bg.jpg');
  background-size: cover;
  background-position: center bottom;
  position: relative;
  padding: 50px 100px 50px 0;
  box-sizing: border-box;
  overflow: hidden;

  &-option {
    float: right;
    height: 100%;

    &-item {
      background: rgba(0, 91, 247, 0.2);
      border-radius: 10px;
      border: 1px solid #D6E5FF;
      box-sizing: border-box;
      padding: 20px;
      margin-bottom: 30px;
      cursor: pointer;

      &:last-child {
        margin-bottom: 0;
      }

      &-box {
        min-width: 284px;
        height: 117px;
        background-color: #FFF;
        border-radius: 8px;
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;

        &-label {
          padding: 20px;

          .vip {
            width: 18px;
            height: 18px;
            margin-right: 10px;
            vertical-align: top;
          }

          .title {
            color: #3B76ED;
            font-size: 30px;
          }
        }

        .selected {
          position: absolute;
          right: 0;
          bottom: 0;
          width: 65px;
        }
      }
    }
  }
}
</style>